package com.quartz.config;

import com.quartz.quartz.AbstractQuartzJob;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created with IntelliJ IDEA.
 * User: lixiao
 * Created Date: 2020/7/31 17:15
 */
@Configuration
public class QuartzConfig {


    @Resource(name = "abstractQuartzJob")
    AbstractQuartzJob abstractQuartzJob;

    @Autowired
    DataSource dataSource;


    @Bean(name = "schedulerFactoryBean")
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException, SQLException {
        SchedulerFactoryBean schedulerFactoryBean=new SchedulerFactoryBean();
        PropertiesFactoryBean factoryBean=new PropertiesFactoryBean();
        factoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        factoryBean.afterPropertiesSet();
        //添加配置文件
        schedulerFactoryBean.setQuartzProperties(factoryBean.getObject());
        schedulerFactoryBean.setAutoStartup(false);
        schedulerFactoryBean.setJobFactory(abstractQuartzJob);
        schedulerFactoryBean.setDataSource(dataSource);
        return schedulerFactoryBean;
    }

    @Bean(name = "getScheduler")
    public Scheduler getScheduler() throws IOException, SQLException {
        return schedulerFactoryBean().getScheduler();
    }


}